package com.mfcxy.admin.controller;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.mfcxy.entity.Blog;
import com.mfcxy.exception.CustomException;
import com.mfcxy.service.IBlogSerivce;
import com.mfcxy.service.ITypeService;

@Controller
@RequestMapping("/admin/blog/")
public class BlogController {
	
	@Autowired
	private IBlogSerivce blogSerivce;
	
	@Autowired
	private ITypeService typeService;
	
	/**
	 * 列表控制器
	 * @param isshow
	 * @param isdel
	 * @param p
	 * @param model
	 * @return
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonParseException 
	 */
	@RequestMapping("list/{p}")
	public String list(@PathVariable("p") int p , Model model) throws JsonParseException, JsonMappingException, IOException {
		model.addAttribute("map", blogSerivce.selectlist(p));
		return "admin/blog_list";
	}
	
	/**
	 * 新增视图 
	 * @return
	 */
	@RequestMapping("addview")
	public String addView(Model model) {
		model.addAttribute("onetype", typeService.listById(0));
		return "admin/blog_add";
	}
	
	/**
	 *  新增博客
	 * @param blog
	 * @return
	 * @throws CustomException 
	 * @throws JsonProcessingException 
	 */
	@RequestMapping(value = "add" , method = RequestMethod.POST)
	public String add(@ModelAttribute Blog blog) throws JsonProcessingException, CustomException {
		blog.checkAdd();
		blogSerivce.save(blog);
		return null;
	}
	
	
	
}
